from uuid import uuid4
from functools import partial
from typing import Callable, Union

from django.db import models


def dynamic_upload(
    path: str,
    instance: Union[models.Model, None] = None,
    filename: Union[str, None] = None,
) -> Union[
        Callable[
            [
                Union[models.Model, None],
                Union[str, None]
            ],
            Union[str, Callable]
        ],
        str
]:
    if not path.endswith('/'):
        path += '/'

    if not filename:
        return partial(dynamic_upload, path)

    filename = f'{uuid4().hex}-{filename}'

    return f'{path}{filename}'
